<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      auto-complete="on"
      label-position="left"
      label-width="0px"
      class="card-box login-form">
      <h3 class="title">登录</h3>
      <el-form-item prop="name">
        <el-input
          v-model="loginForm.name"
          name="name"
          type="text"
          auto-complete="on"
          placeholder="用户名"/>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="handleLogin"/>
        <span class="show-pwd" @click="showPwd"/>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.trim().length < 1) {
        // eslint-disable-next-line callback-return
        callback(new Error('用户名不能为空'));
      } else {
        // eslint-disable-next-line callback-return
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.trim().length < 1) {
        // eslint-disable-next-line callback-return
        callback(new Error('密码不能为空'));
      } else {
        // eslint-disable-next-line callback-return
        callback();
      }
    };

    return {
      loginForm: {
        name: '',
        password: ''
      },
      loginRules: {
        name: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      pwdType: 'password'
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = '';
      } else {
        this.pwdType = 'password';
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          console.log(this.loginForm);
          this.loading = true;
          this.$store.dispatch('Login', this.loginForm).then((res) => {
            console.log(res);
            this.loading = false;
            if (res.isSuccess) {
              this.$message.success(res.failMessage);
              this.$router.push({ path: '/index' });
            } else {
              this.$message.error(res.failMessage);
            }
          }).catch((e) => {
            this.loading = false;
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style>
.el-input__inner{
  height: 150px;
  border: 4px solid #f5f5f5;
}
.el-form-item{
  margin-bottom: 100px;
}

.el-button{
  height:150px ;
}
</style>
